package com.mall.controller;

import com.mall.dto.UmsAdminLoginParam;
import com.mall.pageResult.PageResult;
import com.mall.pojo.UmsAdmin;
import com.mall.pojo.UmsRole;
import com.mall.service.AdminService;
import com.mall.utils.Result;
import com.mall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     *
     * @param umsAdminLoginParam
     * @return
     */
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        Map<String, String> result = adminService.login(umsAdminLoginParam);
        return Result.success(result, "操作成功");
    }

    /**
     * 解析token
     *
     * @return
     */
    @GetMapping("/{token}")
    public Long parseToken(@PathVariable String token) {
        return adminService.parseToken(token);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param name
     * @return
     */
    @GetMapping("info")
    public Result<Map<String, Object>> info(@RequestParam(required = false) String name) {
        Map<String, Object> result = adminService.info();
        return Result.success(result, "操作成功");
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult<UmsAdmin>> list(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                             @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                             @RequestParam(required = false) String keyword) {
        PageResult<UmsAdmin> result = adminService.list(pageNum, pageSize, keyword);
        return Result.success(result, "操作成功");
    }

    /**
     * 用户注册
     *
     * @param umsAdminParam
     * @return
     */
    @PostMapping("/register")
    public Result<UmsAdmin> register(@RequestBody UmsAdmin umsAdminParam) {
        UmsAdmin admin = adminService.register(umsAdminParam);
        return Result.success(admin, "操作成功");
    }

    /**
     * 删除指定用户信息
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result<Integer> delete(@PathVariable Integer id) {
        Integer count = adminService.delete(id);
        return Result.success(count, "操作成功");
    }

    /**
     * 修改指定用户信息
     * @param id
     * @param admin
     * @return
     */
    @PostMapping("/update/{id}")
    public Result<Integer> update(@PathVariable Integer id,@RequestBody UmsAdmin admin) {
        Integer count = adminService.update(id,admin);
        return Result.success(count, "操作成功");
    }

    /**
     * 登出功能
     * @return
     */
    @PostMapping("/logout")
    public Result<Object> logout() {
        ThreadLocalUtil.remove();
        return Result.success(null, "操作成功");
    }

    /**
     * 修改帐号状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public Result<Integer> updateStatus(@PathVariable Integer id, @RequestParam Integer status){
        Integer count = adminService.updateStatus(id,status);
        return Result.success(count,"操作成功");
    }

    /**
     * 获取指定用户的角色
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public Result<List<UmsRole>> role(@PathVariable Integer adminId){
        List<UmsRole> result = adminService.role(adminId);
        return Result.success(result,"操作成功");
    }

    /**
     * 给用户分配角色
     * @param adminId
     * @param roleIds
     * @return
     */
    @PostMapping("/role/update")
    public Result<Integer> updateRole(Integer adminId, Integer[] roleIds){
        Integer count = adminService.updateRole(adminId,roleIds);
        return Result.success(count,"操作成功");
    }
}
